/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package racer.client.gui;

import racer.client.gui.entities.Entity;
import racer.client.gui.entities.CarEntity;
import racer.client.network.NetworkThreadManager;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.HashMap;
//map imports
import racer.client.gui.tiles.Tile;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import racer.client.gui.tiles.TiledMap;

/**
 *
 * @author Piet & Lieven
 */
public class Game extends Canvas {

    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private CarEntity this_player;
    private CarEntity opponent;

    //double want moet met this_player -coördinaten werken,nadien omzetten naar int bij painten
    private double collisionPoint_x=0;
    private double collisionPoint_y=0;
    private double previous_x=0;
    private double previous_y=0;      
    private booleans bol;    
    private boolean gameRunning = true;
    //change here to activate/deactivate
    private boolean mapcollision_active = true;
    private boolean carcollision_active = true;
    //change here to activate/deactivate
    private boolean debugmode_active = true;

   //veelvoud van 5
    static final int hoekveranderingPerKeyPush=20;
    // de strategie die gebruikt moet worden om het veranderen van schermen te versnellen
    private BufferStrategy strategy;
    private static NetworkThreadManager networklayer;
    private Image image_buffer;
    private Graphics bufferScreen;

    //are used!
    private BufferedImage V,H,K1,K2,K3,K4,HFIN,VFIN,CAR2,BALLON,CAR1_40,CAR1_80;
    private BufferedImage CAR1,current_car_image;
    private BufferedImage CAR1_45,CAR1_90,CAR1_20,CAR1_60,CAR1_110,CAR1_135;
    private BufferedImage CAR1_150,CAR1_180,CAR1_200,CAR1_235,CAR1_270,CAR1_290,CAR1_315;
    private int angle=0;    

    //cruciaal bij collision detection (start)
    
    private int active_tile_this_player=1;
    private int active_tile_opponent=1;
    private int previous_active_tile=0;

    //voor uitleg zie declaratie onderaan
    
    private static HashMap map_angleId_bufferedImageObject;
    private static HashMap map_tileId_tileObject;
    private static ImageBank ib;
    
    
    //constructor
    
 /**
 *Constructor of main application.Actions:
  * <br>- Loading images (imagebank)
  * <br>- init. hashmaps && copying some of them to Calculate class
  * <br>- setting GUI (panel,container,...)
  * <br>- adding key/window-listeners
  * <br>- init entities(cars etc)
  * <br>- init booleans etc..
  * <br>
  * @see loadImages()
  * @see initEntities()
  * @see Game#gameLoop()
  * @see initHashMaps()
  * @see Calculate#initMap_tiletype_switchNumber()
  * */       
    public Game() {
        
        ib = new ImageBank();
        //this order!
        loadImages();
        initHashMaps();
        //later sharen via gemeenschappelijke klasse
        Calculate.initMap_tiletype_switchNumber();
        
        JFrame container = new JFrame("Racer");

        JPanel panel = (JPanel) container.getContentPane();
        panel.setPreferredSize(new Dimension(1024, 900));
        panel.setLayout(null);

        setBounds(0, 0, 1024, 900);
        panel.add(this);

        // AWT moet het scherm niet hertekenen, dit doen we zelf
        setIgnoreRepaint(true);
        
        container.pack();
        container.setResizable(true);
        container.setVisible(true);

        container.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        addKeyListener(new InputHandler());

        requestFocus();

        //buffer strategy die AWT toelaat om de graphics te versnellen
        createBufferStrategy(2);

        strategy = getBufferStrategy();
        bol = new booleans();
        
        //visible objects
        initEntities();
    }

 /**
 *Main LOOP of the game application (running the whole time from begin till end of game)
  * <br>- Creates image_buffer (draw on this before copying it to real output using double buffering principle)
  * <br>- Creates backgroundimage (after the map has been drawn tile by tile the whole image is saved)
  * <br>- Builds Map (fill tile hashmap && paints it on backgroundimage)
  * <br>- copying some hashmaps to Calculate class
  * <br>- calculates loop time
  * <br>- starts the real loop (lifetime depending on gameRunning boolean)
  * <br>- calculates map collision if map collision is active
  * <br>- calculates new car position 
  * <br>- calculates car collision if car collision is active
  * <br>- paints some debuginfo (active tile rectangle boundaries,active_tile number,collision point,...)
  * <br> each loop output is painted in layers: 1.bufferscreen =empty 2.backgroundimage (original map) 3.car painted 4.debuginfo painted
  * <br>Last but not least: sending car position coordinates+angle through the network layer to the server
  * @see  Calculate#setMap_tileId_tileObject(HashMap);
  * @see  Calculate#setMap_angleId_bufferedImageObject(HashMap);
  * @see  TiledMap#getTiledMap()
  * @see  Calculate#new_car_position(Game,long)
  * @see  Calculate#mapCollisionCheck(Game);
  * @see  NetworkThreadManager#sendPositionToServer
  * @see  Calculate#carCollisionCheck(Game,double,double)
  * @see  TiledMap#paintActiveTileNumber
  * @see  TiledMap#paintActiveTileBounds
  * @see  paintDebugTextInfo(Graphics g,int x,int y,int w,int h)
  * @see  TiledMap#paintCollisionRectangle
  * @see  TiledMap#paintImagePositionCoordinate
  * @see  carsCloseToEachOther()
  * */    
                   
                        
                        
   private void gameLoop() {
       synchronized (this) {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        
        
        image_buffer = createImage(1024,900);
        //paint map 1 time and save it to image
        //so we can call this image next time instead of repainting each tile
        Image backgroundImage = createImage(1024,900);
        //returns built map(inc tileobjects)
        map_tileId_tileObject = new TiledMap(this,backgroundImage.getGraphics()).getTiledMap();
        
        //copy tileobjects too collision/position class (1 time)
        Calculate.setMap_tileId_tileObject(map_tileId_tileObject);
        Calculate.setMap_angleId_bufferedImageObject(map_angleId_bufferedImageObject);
        
        
        long lastLoopTime = System.currentTimeMillis();
        //beperking netwerkverkeer zie verder sendpositionto..
        int previous_angle=0;
        
        while (gameRunning){
            
            long delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();
            
            //dependant on moving direction...position coordinates will be adapted
            //calculate_new_car_position(delta);
            Calculate.new_car_position(this,delta);
            //x en ycoordinaten verzetten rekening houdend met de snelheid
            //dx=dy=snelheid=pixel per seconde=constante=150
            //delta zorgt voor synchronisatie (tijd)
            //dus het bepaalt de volgende x en y coordinaat adhv een constante en de richting(moveSpeed)

            bufferScreen = image_buffer.getGraphics();  
            bufferScreen.drawImage(backgroundImage, 0, 0, null);

      

            
            for (Entity entity : entities) {
                //if ((entity.getHorizontalMovement()== 0)&&(entity.getVerticalMovement()==0)){}else{
                //eerst size terug goedzetten(voor elke rotatiepic anders

                    if (entity.getId() == 1){

                       if(mapcollision_active){
                            Calculate.mapCollisionCheck(this);
                       } 
                       
                        
                        //deze beperking is nodig;anders stuurt de applicatie bij elke loop een positiebericht
                        //niet nodig als de auto stilstaat
                        //aangezien men kan stilstaan(as in: niet gasgeven) en toch van richting veranderen
                        //is er een controle op een eventuele gewijzigde hoek
                        //
                        
                        //BUG?als ik deze uiterste if weglaat springt de 2 de client bij het opstarten(auto) naar links
                        //als ik previ...angle..; != angle weglaat stuurt hij het niet door als auto van hoek verandert
                        //later fixen
                        if(previous_angle==0){
                            if ((entity.getHorizontalMovement()== 0)&&(entity.getVerticalMovement()==0)){}else{
                                networklayer.sendPositionToServer(entity.getX(),entity.getY(),angle,active_tile_this_player);                    
                            }                            
                        }else{


                            if ((entity.getHorizontalMovement()== 0)&&(entity.getVerticalMovement()==0)&&(previous_angle != angle)){}else{
                                networklayer.sendPositionToServer(entity.getX(),entity.getY(),angle,active_tile_this_player);                    
                            }
                        }
                        
                    //PAINT order important(paints over each other)
                    if(debugmode_active){
                        TiledMap.paintActiveTileNumber(bufferScreen,((Tile)map_tileId_tileObject.get(active_tile_this_player)).x,((Tile)map_tileId_tileObject.get(active_tile_this_player)).y,((Tile)map_tileId_tileObject.get(active_tile_this_player)).width,((Tile)map_tileId_tileObject.get(active_tile_this_player)).height,Integer.toString(active_tile_this_player));
                        TiledMap.paintActiveTileBounds(bufferScreen,((Tile)map_tileId_tileObject.get(active_tile_this_player)).x,((Tile)map_tileId_tileObject.get(active_tile_this_player)).y,((Tile)map_tileId_tileObject.get(active_tile_this_player)).width,((Tile)map_tileId_tileObject.get(active_tile_this_player)).height);
                        paintDebugTextInfo(bufferScreen,((Tile)map_tileId_tileObject.get(active_tile_this_player)).x,((Tile)map_tileId_tileObject.get(active_tile_this_player)).y,((Tile)map_tileId_tileObject.get(active_tile_this_player)).width,((Tile)map_tileId_tileObject.get(active_tile_this_player)).height);
                    }
                    entity.drawPic(bufferScreen,current_car_image);    
                    if(debugmode_active){
                        TiledMap.paintCollisionRectangle(bufferScreen,Integer.valueOf((int)collisionPoint_x),Integer.valueOf((int)collisionPoint_y),5,5,Color.YELLOW);
                        TiledMap.paintImagePositionCoordinate(bufferScreen,Integer.valueOf((int)entity.getX()),Integer.valueOf((int)entity.getY()),2,2,Color.GREEN);
                    }
                }else if(entity.getId() == 2){
                    if(debugmode_active){
                        TiledMap.paintActiveTileBounds(bufferScreen,((Tile)map_tileId_tileObject.get(active_tile_opponent)).x,((Tile)map_tileId_tileObject.get(active_tile_opponent)).y,((Tile)map_tileId_tileObject.get(active_tile_opponent)).width,((Tile)map_tileId_tileObject.get(active_tile_opponent)).height);  
                        TiledMap.paintActiveTileNumber(bufferScreen,((Tile)map_tileId_tileObject.get(active_tile_opponent)).x,((Tile)map_tileId_tileObject.get(active_tile_opponent)).y,((Tile)map_tileId_tileObject.get(active_tile_opponent)).width,((Tile)map_tileId_tileObject.get(active_tile_opponent)).height,Integer.toString(active_tile_opponent));  
                    }
                        entity.drawPic(bufferScreen,(BufferedImage)map_angleId_bufferedImageObject.get(entity.getAngle()));    
                }
                    bufferScreen.setColor(Color.BLACK);
                    //only collision check when both cars are close to each other (max 1 tile in between = on)
                    if(debugmode_active){
                        bufferScreen.drawString("active tiles between players:"+Math.abs(active_tile_this_player - active_tile_opponent), 200,350);
                    }
                 
                    if(carcollision_active){
                         if(carsCloseToEachOther()){
                         //if((Math.abs(active_tile_this_player - active_tile_opponent) <=1)||(Math.abs(active_tile_this_player - active_tile_opponent) >= (map_tileId_tileObject.size()-2)))   {
                             if(debugmode_active){
                                bufferScreen.drawString("monitoring car collision: ON", 200,370);
                             }
                             Calculate.carCollisionCheck(this,previous_x,previous_y);
                         }else{
                             if(debugmode_active){
                                 bufferScreen.drawString("monitoring car collision: OFF", 200,370);        
                             }
                         }
                    }
                    
                
                

                
            }
            bufferScreen.dispose();
            g.drawImage(image_buffer,0,0,this);    
            
            strategy.show();
            previous_angle=angle;
            //for this_player collision reset
            previous_x=this_player.getX();
            previous_y=this_player.getY();
            try { Thread.sleep(10); } catch (Exception e) {}
        }
    }



}
   
 /**
 *Calculates if there is need for car collision check
  * <br>checks if both cars are close to each other by using active_tile numbers of player 1 and opponent (player 2)
  * 2 things:<br> 1.simply by deduction of both integers for normal check. <br>
  * Extra check for active_tile1 at the beginning && active_tile2 at the end of the map (f.e. 20 tiles...so tile abs(1-20)=19 <br>
  * as u see this is not correct,they are less then 1 close towards each other. <br>
  * Solution: deduction of both active_tiles must be smaller than 1 or greater/equal to (maxtile-2) to ENABLE car collision (19 > (20-2))
  * @return boolean cars close to each other? 
  * @see Tile
  * @see Calculate#carCollisionCheck(Game game,double previous_x,double previous_y)
  * @see Game#gameLoop()
  * */   
 private boolean carsCloseToEachOther(){
     if((Math.abs(active_tile_this_player - active_tile_opponent) <=1)||(Math.abs(active_tile_this_player - active_tile_opponent) >= (map_tileId_tileObject.size()-2)))   {
         return true;
     }else{
         return false;
     }
 }
  /**
  * Methods to handle KeyBoard interaction
  * 
  * */    


    private class InputHandler extends KeyAdapter {
        
        
        //links rechts moet richting bepalen
        //top down moet bewegen...dus als je naar rechts wilt..rechts voor richting
        //en als je vooruit duwt rekening houden met richting ..hoek
        /**
         * Sets booleans when keyboard keys are being pressed.
         * @see Calculate#new_car_position(Game game,long delta)
         * @see booleans
         * */
        @Override
        public void keyPressed(KeyEvent e) {
            //na het setten van de wagens mag mapcollision af (anders botst het)
            //eerst knop induwen
            //mapcollision_active=true;
            //carcollision_active=true;
            synchronized (this) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                
                angle-=hoekveranderingPerKeyPush;
                if (angle <0){
                    angle=360+angle;
                }
                else if (angle >=360){
                    angle-=360;
                }                    
                 
                System.out.println("angle:"+angle);
                current_car_image=(BufferedImage)map_angleId_bufferedImageObject.get(angle);
                
                if((current_car_image == CAR1)||(current_car_image == CAR1_90)||(current_car_image == CAR1_180)||(current_car_image == CAR1_270)){
                    
                bol.setmovingDiagonalOnScreen(false);                
                
                }else{
                    bol.setmovingDiagonalOnScreen(true);
                    
                
                }                    
                
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
           
                angle+=hoekveranderingPerKeyPush;
                 
                if (angle <0){
                    angle=360+angle;
                }
                else if (angle >=360){
                    angle-=360;
                }                    
                    System.out.println("angle:"+angle);
                    current_car_image=(BufferedImage)map_angleId_bufferedImageObject.get(angle);                
                 
                if((current_car_image == CAR1)||(current_car_image == CAR1_90)||(current_car_image == CAR1_180)||(current_car_image == CAR1_270)){
                bol.setmovingDiagonalOnScreen(false);    
                            

                    
                }else{
                    bol.setmovingDiagonalOnScreen(true);
                    
                }                                
        
                                                                  
                
                
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                //upPressed = true;
                
                
                bol.setvooruitrijden(true);
                bol.setachteruitrijden(false);
                
                //upPressed = false;
                if(current_car_image == CAR1){
                    //movingLeftOnScreen = true;
                    bol.setmovingLeftOnScreen(true);
                }else if(current_car_image == CAR1_90){
                    //movingUpOnScreen = true;
                    bol.setmovingUpOnScreen(true);
                }else if(current_car_image == CAR1_180){
                    //movingRightOnScreen = true;
                    bol.setmovingRightOnScreen(true);
                }else if (current_car_image == CAR1_270){
                    //movingDownOnScreen = true;
                    bol.setmovingDownOnScreen(true);
                    
                }else{
                    //movingDiagonalOnScreen = true;
                    bol.setmovingDiagonalOnScreen(true);
                    
                }
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                //downPressed = true;

                
                bol.setvooruitrijden(false);
                bol.setachteruitrijden(true);

                if(current_car_image == CAR1){
                    bol.setmovingRightOnScreen(true);
                }else if(current_car_image == CAR1_90){
                    bol.setmovingDownOnScreen(true);
                }else if(current_car_image == CAR1_180){
                    bol.setmovingLeftOnScreen(true);
                }else if (current_car_image == CAR1_270){
                    bol.setmovingUpOnScreen(true);
                }else{
                   bol.setmovingDiagonalOnScreen(true);

                }                
                
            
            }}
        }

        @Override
        public void keyReleased(KeyEvent e) {
            synchronized (this) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {

                

            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

        
                

            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                //upPressed = false;
                bol.setvooruitrijden(false);
                
               if(current_car_image == CAR1){
                   bol.setmovingLeftOnScreen(false);
                }else if(current_car_image == CAR1_90){
                    
                    bol.setmovingUpOnScreen(false);
                }else if(current_car_image == CAR1_180){
                    bol.setmovingRightOnScreen(false);
                }else if (current_car_image == CAR1_270){
                    
                    bol.setmovingDownOnScreen(false);
                }else{

                }
 
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                //downPressed(false);
                bol.setachteruitrijden(false);
                if(current_car_image == CAR1){
                    
                    bol.setmovingRightOnScreen(false);
                }else if(current_car_image == CAR1_90){
                    
                    bol.setmovingDownOnScreen(false);
                }else if(current_car_image == CAR1_180){
                    
                    bol.setmovingLeftOnScreen(false);
                }else if (current_car_image == CAR1_270){
                    
                    bol.setmovingUpOnScreen(false);
                }else{
                   // movingDiagonalOnScreen = false;
                   // System.out.println("movingDiagonalOnScreen = false,DOWN key los");
                }                     
                
            }}
        }

        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == 27) {
                System.exit(0);
            }
        }
    }
    


  /*
  * Methods used by network layer f.e.
  * 
  * */
    public HashMap<String,String> getSettings(){
        
        return networklayer.settingsmap;
        
    }    
    public void setOpponentPosition(double x, double y,int angle,int activetile_opponent){
        opponent.setX(x);
        opponent.setY(y);
        opponent.setAngle(angle);
        active_tile_opponent = activetile_opponent;
    }    
    
    
  /*
  * Init. Methods
  * 
  * */
    public void loadImages(){
            
           K1 = ib.getImage("b1");
           K2 = ib.getImage("b2");
           K3 = ib.getImage("b3");
           K4 = ib.getImage("b4");
           V = ib.getImage("vert");
           H = ib.getImage("hori");
           VFIN = ib.getImage("vertifin");
           HFIN = ib.getImage("horifin");
           CAR1 = ib.getImage("car1");
           BALLON = ib.getImage("ballon");

           CAR2 = ib.getImage("car2");
           CAR1_20 = ib.getImage("car1_20");
           CAR1_40 = ib.getImage("car1_40");
           CAR1_45 = ib.getImage("car1_45");
           CAR1_60 = ib.getImage("car1_60");
           CAR1_80 = ib.getImage("car1_80");
           CAR1_90 = ib.getImage("car1_90");
           CAR1_110 = ib.getImage("car1_110");
           CAR1_135 = ib.getImage("car1_135");
           CAR1_150 = ib.getImage("car1_150");
              
           CAR1_180 = ib.getImage("car1_180");
           CAR1_200 = ib.getImage("car1_200");
           CAR1_235 = ib.getImage("car1_235");
           CAR1_270 = ib.getImage("car1_270");
           CAR1_290 = ib.getImage("car1_290");
           CAR1_315 = ib.getImage("car1_315");        
    }
    public ImageBank getImageBank(){
        return ib;
    }    
    public void initHashMaps(){

           //hier komen alle tiles in met hun id,coordinaten van hun linkerbovenhoek,
           //type tile en een buurobject die de ids van al zijn buren bijhoudt (in elke richting)
           map_tileId_tileObject=new HashMap();
           map_angleId_bufferedImageObject=new HashMap();
           HashMap tempmap_angleId_bufferedImageObject=new HashMap();

           //existing pics
           tempmap_angleId_bufferedImageObject.put(0,CAR1);
           tempmap_angleId_bufferedImageObject.put(1,CAR1_20);
           tempmap_angleId_bufferedImageObject.put(2,CAR1_45);
           tempmap_angleId_bufferedImageObject.put(3,CAR1_60);
           tempmap_angleId_bufferedImageObject.put(4,CAR1_90);
           tempmap_angleId_bufferedImageObject.put(5,CAR1_110);
           tempmap_angleId_bufferedImageObject.put(6,CAR1_135);
           tempmap_angleId_bufferedImageObject.put(7,CAR1_150);
           tempmap_angleId_bufferedImageObject.put(8,CAR1_180);
           tempmap_angleId_bufferedImageObject.put(9,CAR1_200);
           tempmap_angleId_bufferedImageObject.put(10,CAR1_235);
           tempmap_angleId_bufferedImageObject.put(11,CAR1_270);
           tempmap_angleId_bufferedImageObject.put(12,CAR1_290);
           tempmap_angleId_bufferedImageObject.put(13,CAR1_315);                   
           tempmap_angleId_bufferedImageObject.put(14,CAR1);
        
           int angle_counter=0;
           //elke toets wordt er 5 bijgeteld
           //360/5=72 ....hoe meer elementen in de hash per hoek,hoe (realistischer)trager het gaat
           //bij het rijden
           //om de 5 hashelementen..nieuw hoek (wat uittesten) =>5*5=25
           
           //divide all degrees by all pics..so more >1 degree for each pic
           
           for (int i=0;i<360;i+=5){
                if(i %25 == 0){
                    angle_counter++;
                }
               map_angleId_bufferedImageObject.put(i,tempmap_angleId_bufferedImageObject.get(angle_counter));
           }
           //overwrite 
           map_angleId_bufferedImageObject.put(0,CAR1);
           map_angleId_bufferedImageObject.put(340,CAR1_315);
           map_angleId_bufferedImageObject.put(350,CAR1);
           map_angleId_bufferedImageObject.put(355,CAR1);
    }

    private void initEntities() {	
             //voorlopig worden posites gezet in het begin maar worden later gewizjigd afh van soort map 
        //en positie finish line
        this_player = new CarEntity(this,1,250,110, Color.BLUE);
        current_car_image=CAR1;
        opponent = new CarEntity(this,2,250,160, Color.RED);
	entities.add(this_player);
        entities.add(opponent);
    }

    
  
 /*
  * Methods used by in debug mode
  * 
  * */
    //al in tiledmap
    public void paintDebugTextInfo(Graphics g,int x,int y,int w,int h){
        
            g.setColor(Color.BLACK);
            g.drawString("CHECK WHERE IT GOES WRONG (combineren van richtingen+collision)", 60,650);
            g.drawString("map collision active?:"+mapcollision_active, 60,670);
            g.drawString("car collision active?:"+carcollision_active, 60,690);
            g.drawString("fictieve hoek (angle ~ per draai +20°):"+angle+"°", 200,20);
            g.drawString("collisionPoint_x:"+collisionPoint_x, 200,40);
            g.drawString("Tile interval x-as:"+x+","+(x+w), 200,60);
            g.drawString("collisionPoint_y:"+collisionPoint_y, 200,80);
            g.drawString("Tile interval y-as:"+y+","+(y+h), 200,100);
            g.drawString("movingDiagonalOnScreen:"+bol.getmovingDiagonalOnScreen(), 500,60);
            
            
            g.setColor(Color.RED);
            g.drawString("DEBUGGING MODE ON",500,80);
            g.drawString("rood = tile omkadering", 500, 100);
            g.drawString("zwart = tile ID", 500, 120);
            g.drawString("geel = collision point (coördinaat voor trigger active tile en collision (auto tov tiles))", 500, 140);
            g.drawString("groen = paint reference point (coördinaat voor painten van afbeelding auto)", 500, 160);        
    }




 /*
  * Methods used by calculate class (collision + new position)
  * 
  * */

    public void setThisPlayer(CarEntity this_player){
        this.this_player=this_player;
    }
    public CarEntity getThisPlayer(){
        return this_player;
    }    
    public CarEntity getOpponent(){
        return opponent;
    }        
    public void setActiveTile(int active_tile){
        this.active_tile_this_player=active_tile;
    }    
    public void setCollisionXY(double collisionx,double collisiony){
        collisionPoint_x=collisionx;
        collisionPoint_y=collisiony;
    }
    public BufferedImage getCurrentCarImage(){
        return current_car_image;
    }        
    public int getActiveTile(){
        return active_tile_this_player;
    }    
    public void setPreviousActiveTile(int previous_active_tile){
        this.previous_active_tile=previous_active_tile;
    }    
    public int getPreviousActiveTile(){
        return previous_active_tile;
    }        
    public booleans getBooleans(){
        return bol;
    }
    public int getAngle(){
        return angle;
    }
    public HashMap getMap_tileId_tileObject(){
        return map_tileId_tileObject;
    }        
    
    
    
    
    
    
    public static void main(String[] args) {
        
        Game g = new Game();
        String serverAddress = JOptionPane.showInputDialog("Geef het adres van de server.","localhost");

        networklayer = new NetworkThreadManager(2222,serverAddress, g);
        
        //nick wordt in onderste laag verstuurd
        String nickname = JOptionPane.showInputDialog("Geef een nickname","");
        networklayer.setSetting("nick",nickname);
        //nickid vragen aan server aan de hand van nick
        //id nodig voor messages en achterliggende functionaliteit
        //nickname is naar de spelers toe
        String nickid="1";
        networklayer.setSetting("nickid",nickid);
        networklayer.setMessageIDs();
        networklayer.sendSettingMessageToServer(0,"nickname:"+nickname);
        networklayer.sendSettingMessageToServer(0,"nickid:"+nickid);

        g.gameLoop();
        
    }
  
       

}
